--
-- Author: shunguo.chen
-- Date: 2019-05-16 16:47:06
--
-- LegendPanel  传奇武器
--

local Armature = require("app.public.avatar.Armature")
local TabsBtn = require("app.widget.TabsBtn")
local IconRender = require("app.widget.renders.IconRender")
local ListViewVertical = require("app.widget.listview.ListViewVertical")

local LegendRender = import(".renders.LegendRender")
local LegendSkillRender = import(".renders.LegendSkillRender")

local ClassRef = app.mvc.createPanelClass(...)

local COLOR_CONDITION_MEET = cc.c3b(240, 222, 129)
local COLOR_CONDITION_NOT_MEET = display.COLOR_RED
local EXP_ACTION_GAP = 0.1

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    local panel = self._ui.Panel

    -- left legends --
    local params = {
        renderClass = LegendRender,
        perNum = 1,
        isMultipleLayer = false,
        cellEventProxy = function(eventName, render)
            local modelIndex = render:getCellModelIndex()
            self._legendSV:setFoucsIndex(modelIndex)
            self._preLv = nil
            self._curLegend = render:getData()
            self:_refreshLegend()
        end,
    }
    self._legendSV = ListViewVertical.new(self._ui.ScrollView, params)

    -- middle legend display --
    local midPanel = self._ui.Panel_arms
    midPanel:getChildByName("btn_see"):addClickEventListener(function()
        executeFunc(self._onOperate, "legend_preview", self._curLegend)
    end)
    self._legendIcon = midPanel:getChildByName("img")
    self._legendNameText = midPanel:getChildByName("name")
    self._legendTipText = midPanel:getChildByName("lv")
    self._useBtn = midPanel:getChildByName("Button_use")
    self._useBtnText = self._useBtn:getChildByName("name")
    self._useBtn:addClickEventListener(function()
        if self._curLegend:isActive() then
            executeFunc(self._onOperate, "legend_equip", self._curLegend)
        else
            executeFunc(self._onOperate, "legend_get", self._curLegend)
        end
    end)
    self._unuseBtn = midPanel:getChildByName("Button_unload")
    self._unuseBtn:addClickEventListener(function()
        executeFunc(self._onOperate, "legend_equip", self._curLegend)
    end)

    local tipPanel = self._ui.tips:getChildByName("Panel")
    display.nodeRemoveForNotHit(tipPanel, function()
        self:_hideTips()
    end)

    -- right legend base --
    local basePanel = self._ui.data:getChildByName("Panel")

    -- right bottom
    local bomPanel = basePanel:getChildByName("Panel_bom")
    self._bomPanel = bomPanel
    self._bomTip = bomPanel:getChildByName("tips")
    self._leftBtn = bomPanel:getChildByName("Button_1")
    self._leftBtnText = self._leftBtn:getChildByName("name")
    self._leftBtn:addClickEventListener(handler(self, self._onClickLeftBtn))

    self._rightBtn = bomPanel:getChildByName("Button_2")
    self._rightBtnText = self._rightBtn:getChildByName("name")
    self._rightBtn:addClickEventListener(handler(self, self._onClickRightBtn))
    self._rightBtn:addTouchEventListener(handler(self, self._onTouchRightBtn))

    -- right lvPanel
    local lvPanel = basePanel:getChildByName("Panel_contract")
    self._lvIconRender = IconRender.create(lvPanel:getChildByName("icon"))
    self._lvIconRender:setTipEnable(true)
    self._lvIconRender:setDefaultShowNumLimit(1)
    self._lvExpText = lvPanel:getChildByName("exp_num")
    self._lvText = lvPanel:findChild("levelPanel/lv_num")
    lvPanel:getChildByName("Button_explain"):addClickEventListener(function(sender)
        self:_showTips(sender)
    end)

    local sprite = lvPanel:getChildByName("exp") 
    local pgSprite = cc.ProgressTimer:create(sprite)
    pgSprite:setPosition(sprite:getPosition())
    sprite:removeFromParent()
    lvPanel:addChild(pgSprite)
    self._lvPgSprite = pgSprite

    local aniNode = lvPanel:getChildByName("Node_ani")
    local aniWorldPos = aniNode:convertToWorldSpaceAR(cc.p(0,0))
    self._lvAniPos = self._ui.Panel:convertToNodeSpaceAR(aniWorldPos)

    -- right corePanel
    local corePanel = basePanel:getChildByName("Panel_core")
    self._coreSkillIcon = corePanel:getChildByName("icon")
    self._coreSkillNameText = corePanel:getChildByName("name")
    self._coreCurPanel = corePanel:getChildByName("text_1")
    self._coreNextPanel = corePanel:getChildByName("text_2")
    display.setCascadeOpacityEnabled(self._coreNextPanel, true)
    corePanel:getChildByName("Button_explain"):addClickEventListener(function(sender)
        self:_showTips(sender)
    end)

    -- right skillPanel
    local skillPanel = basePanel:getChildByName("Panel_skill")
    self._skillTypeText = skillPanel:getChildByName("tlt")
    self._skillNumText = skillPanel:getChildByName("num")
    self._skillTipBtn = skillPanel:getChildByName("Button_explain")
    self._skillTipBtn:addClickEventListener(function(sender)
        self:_showTips(sender)
    end)

    local params = {
        renderClass = LegendSkillRender,
        perNum = 1,
        isMultipleLayer = false,
        firstEnterAsyncLoad = false,
        cellEventProxy = handler(self, self._onSkillEvent),
    }
    self._skillSV = ListViewVertical.new(skillPanel:getChildByName("ScrollView"), params)

    -- right top
    local btns = {
        basePanel:findChild("Panel_top/Button_1"),
        basePanel:findChild("Panel_top/Button_2"),
        basePanel:findChild("Panel_top/Button_3"),
    }
    for i,btn in ipairs(btns) do
        btn:setTag(i)
    end
    local binds = {lvPanel, corePanel, skillPanel}

    local function onTabsBtn(index)
        if self._curIdx then
            self._curIdx = index
            self:switchSubPanel()
        else
            self._curIdx = index
        end
    end
    local function onSelectBtn(btn, b)
        local tag = btn:getTag()
        local nameText = basePanel:findChild("Panel_top/name_" .. tag)
        if nameText then
            nameText:setOpacity(b and 255 or 125)
        end
    end
    self._tabsBtn = TabsBtn.new(btns, onTabsBtn, 1, binds, onSelectBtn)
    self._dots = {
        basePanel:findChild("Panel_top/dot_1"),
        basePanel:findChild("Panel_top/dot_2"),
        basePanel:findChild("Panel_top/dot_3"),
    }

    -- right legend fashion --
    local fashionPanel = self._ui.make:getChildByName("Panel")
    self._makeBomPanel = fashionPanel:getChildByName("Panel_bom")
    self._makeTip = fashionPanel:findChild("Panel_bom/tips")
    self._makeBtn = fashionPanel:findChild("Panel_bom/Button_2")
    self._makeBtn:addClickEventListener(handler(self, self._onClickMakeBtn))

    self._makeCorePanel = fashionPanel:getChildByName("Panel_core")
    self._makeMaxPanel = fashionPanel:getChildByName("Panel_max")

    self._makeNeedText1 = fashionPanel:findChild("Panel_core/lv")
    self._makeNeedText2 = fashionPanel:findChild("Panel_core/tips")
    self._makeNeedText1PosY = self._makeNeedText1:getPositionY()
    self._makeNeedText2PosY = self._makeNeedText2:getPositionY()

    local makePanel = fashionPanel:findChild("Panel_core/Panel_icon_4")
    local itemRenders = {}
    local itemBgs = {}
    for i=1,4 do
        local render = IconRender.create(makePanel:getChildByName("icon_" .. i))
        render:setTipEnable(true)
        itemRenders[#itemRenders + 1] = render
        itemBgs[#itemBgs + 1] = makePanel:getChildByName("bg_" .. i)
    end
    self._makeItemRenders = itemRenders
    self._makeItemBgs = itemBgs
    self._makeTargetRender = IconRender.create(makePanel:getChildByName("icon"))
    self._makeTargetRender:setTipEnable(true)

    local aniNode = self._makeCorePanel:getChildByName("Node_ani")
    local aniWorldPos = aniNode:convertToWorldSpaceAR(cc.p(0,0))
    self._makeAniPos = self._ui.Panel:convertToNodeSpaceAR(aniWorldPos)

    -- for test
    if DEBUG > 0 then
        self._directHandler = EM:on("onDirectKey", handler(self, self.testDirect))
    end
end

function ClassRef:remove()
    if DEBUG > 0 then
        EM:remove(self._directHandler)
    end
end

function ClassRef:getGuideNode(id)
    if id == 511 then
        -- 选择技能页签
        return self._tabsBtn:getBtn(3)
    elseif id == 512 then
        -- 激活神器技能
        local render = self._skillSV:getCellByIndex(1)
        if render then
            return render:getActiveBtn()
        end
    elseif id == 513 then
        -- 查看技能提示
        return self._skillTipBtn
    end
end

function ClassRef:reOpen()
    if self._curIdx == 3 then
        self:refreshSkillPanel()
    end
end

-- legendInfo = LegendInfo
-- panelIdx = 1(基础) 3（时装）
-- subPanelIdx = 1 契约 2 核心 3 技能
function ClassRef:refreshList(legendInfo, panelIdx, subPanelIdx)
    self._legendInfo = legendInfo
    self._panelIdx = panelIdx

    -- refresh left
    local legends = legendInfo.legends
    local selectIdx, wearIdx
    for i,legend in ipairs(legends) do
        if self._curLegend == legend then
            selectIdx = i
            break
        end
        if legend:isWear() then
            wearIdx = i
        end
    end

    -- 当前的 》 穿戴的 》 默认的
    selectIdx = selectIdx or wearIdx or 1

    self._legendSV:reloadData(legends)
    self._legendSV:setFoucsIndex(selectIdx)
    self._curLegend = legends[selectIdx]

    -- refresh legend
    self:_refreshLegend(subPanelIdx)
end

function ClassRef:testDirect(event, code)
    local armature
    if self._legendArmatureEx then
        armature = self._curLegend:isWear() and self._legendArmatureEx or self._legendArmature
    else
        armature = self._legendArmature
    end
    if not armature then return end

    local posX, posY = armature:getPosition()
    if code == 26 then
        posX = posX - 1
    elseif code == 27 then
        posX = posX + 1
    elseif code == 28 then
        posY = posY + 1
    elseif code == 29 then
        posY = posY - 1
    end

    print("testDirect posX, posY", posX, posY, posX - self._initPosX, posY - self._initPosY)
    armature:setPosition(posX, posY)
end

function ClassRef:_refreshAvatar(param)
    if self._avatarParam == param then return end

    local armature = self._legendArmature
    if armature then
        armature:removeFromParent()
    end
    local armatureEx = self._legendArmatureEx
    if armatureEx then
        armatureEx:removeFromParent()
    end

    armature = Armature.create(param)
    armature:setRotation(param.rotation)
    armature:setScale(param.scale)
    armature:playDefault()
    self._ui.Panel_arms:addChild(armature)

    local posX, posY = self._legendIcon:getPosition()
    local classId = self._curLegend:getClass()
    local offsetPos, offsetPosEx = self._curLegend:getAvatarOffsetPos()
    armature:setPosition(posX + offsetPos[1], posY + offsetPos[2])
    if classId == 4 then
        -- 如果是刺客，显示两把武器
        armatureEx = Armature.create(param)
        armatureEx:setScale(param.scale)
        armatureEx:setRotation(param.rotationEx)
        armatureEx:setPosition(posX + offsetPosEx[1], posY + offsetPosEx[2])
        armatureEx:playDefault()
        self._ui.Panel_arms:addChild(armatureEx)

        self._legendArmatureEx = armatureEx
    end

    self._initPosX, self._initPosY = posX, posY
    self._avatarParam = param
    self._legendArmature = armature
end

function ClassRef:_refreshLegend(subPanelIdx)
    local legend = self._curLegend
    local showFashion = legend:getShowFashion()
    self._legendNameText:setString(legend:getName())
    -- self._legendIcon:loadTexture(showFashion:getBigIcon())
    self._legendIcon:setVisible(false)
    self:_refreshAvatar(showFashion:getAvatarParam())

    self._ui.data:setVisible(self._panelIdx == 1)
    self._ui.make:setVisible(self._panelIdx == 3)

    local isActive = legend:isActive()
    if self._panelIdx == 1 then
        local isWear = legend:isWear()
        self._unuseBtn:setVisible(isWear)
        self._useBtn:setVisible(not isWear)
        self._legendTipText:setVisible(false)
        self._useBtnText:setString(isActive and L("LegendBasic0_ui6") or L("LegendBasic0_ui3"))

        self:switchSubPanel(subPanelIdx)
    else
        self._unuseBtn:setVisible(false)
        self._useBtn:setVisible(not isActive)
        self._legendTipText:setVisible(isActive)
        if isActive then
            self._legendTipText:setString(string.format(L("LegendBasic0_ui5"), showFashion.lv))
        else
            self._useBtnText:setString(L("LegendBasic0_ui3"))
        end

        self:refreshFashionPanel() 
    end
end

------------------ 传奇武器基础培养 --------------------
-- 1 契约
-- 2 核心
-- 3 技能
function ClassRef:switchSubPanel(idx)
    if idx then
        self._tabsBtn:setSelected(idx)
        self._curIdx = idx
    end

    if self._curIdx == 1 then
        self._leftBtnText:setString(L("LegendBasic1_ui2"))
        self._rightBtnText:setString(L("LegendBasic1_ui3"))
        self:refreshLvPanel()
    elseif self._curIdx == 2 then
        self:refreshCorePanel()
        
        -- 清理isNew
        if self._curLegend:isNew() then
            self._curLegend:setIsNew(false)
        end
    else
        self._leftBtnText:setString(L("LegendBasic3_ui1"))
        self._rightBtnText:setString(L("LegendBasic3_ui2"))
        self:refreshSkillPanel()
    end

    self:refreshRedPoint()

    -- refresh bomPanel
    local isActive = self._curLegend:isActive()
    self._bomPanel:setVisible(self._curIdx ~= 2)
    self._bomTip:setVisible(not isActive)
    self._leftBtn:setVisible(isActive)
    self._rightBtn:setVisible(isActive)
end

function ClassRef:refreshRedPoint()
    self._dots[2]:setVisible(self._curIdx ~= 2 and self._curLegend:isNew())
    self._dots[3]:setVisible(self._curLegend.skillPoint > 0)
end

function ClassRef:_onClickLeftBtn()
    if self._curIdx == 1 then
        executeFunc(self._onOperate, "legend_reset", self._curLegend)
    elseif self._curIdx == 3 then
        executeFunc(self._onOperate, "legend_resetSkill", self._curLegend)
    end
end

function ClassRef:_onClickRightBtn()
    if self._curIdx == 3 then
        executeFunc(self._onOperate, "legend_gotoSkill", self._curLegend)
    end
end

function ClassRef:_onTouchRightBtn(sender, eventType)
    if self._curIdx ~= 1 then return end

    if eventType == ccui.TouchEventType.began then
        self:_touchBegan()
    elseif eventType == ccui.TouchEventType.canceled then
        if self._isInTouch then
            self:_touchEnd()
        else
            self:_clearTouchAction()
        end
    elseif eventType == ccui.TouchEventType.ended then
        self:_touchEnd()
    end
end

function ClassRef:_onUpdate()
    self._isInTouch = true
    if self._curLegend:isMaxLv() then
        display.pushToast(L("LegendBasic1_tips1"))
        self:_clearTouchAction()
    elseif self._lvMaxNum == 0 then
        display.pushToast(L("tips_1000093"))
        self:_clearTouchAction()
    else
        self._lvUseNum = self._lvUseNum + 1
        self._lvMaxNum = self._lvMaxNum - 1

        local exp, lv, expMax = self._curLegend:addExp(self._itemExp)
        self:_playPgAction(exp, lv, expMax)
        -- self:_setPg(exp, lv, expMax)

        self._lvIconRender:setNum(self._lvMaxNum)
    end
end

function ClassRef:_touchBegan()
    self._lvUseNum = 0
    self._isInTouch = false
    self:_clearTouchAction()

    -- 长按1s后算
    self._longPressAction = self._rightBtn:performWithDelay(function()
        self._longPressAction = nil
        self._touchAction = self._rightBtn:schedule(handler(self, self._onUpdate), EXP_ACTION_GAP)
        self:_onUpdate()
    end, 1)
end

function ClassRef:_touchEnd()
    if self._isInTouch then
        if self._lvUseNum > 0 then
            executeFunc(self._onOperate, "legend_lvup", self._curLegend, self._lvUseNum)
        end
    else
        if self._curLegend:isMaxLv() then
            display.pushToast(L("LegendBasic1_tips1"))
        elseif not self._lvItemOK then
            display.pushToast(L("tips_1000093"))
        else
            executeFunc(self._onOperate, "legend_lvup", self._curLegend, 1)
        end
    end

    self._lvUseNum = 0
    self._isInTouch = false
    self:_clearTouchAction()
end

function ClassRef:_clearTouchAction()
    if self._longPressAction then
        self._rightBtn:stopAction(self._longPressAction)
        self._longPressAction = nil
    end
    if self._touchAction then
        self._rightBtn:stopAction(self._touchAction)
        self._touchAction = nil
    end
end

function ClassRef:_playPgAction(exp, lv, expMax)
    local percent = 100
    if expMax > 0 then 
        percent = exp/expMax * 100
        self._lvExpText:setString(string.format("%s/%s", exp, expMax))
    else
        self._lvExpText:setString(L("LegendBasic1_ui4"))
    end
    self._lvText:setString(lv)
    -- self._lvPgSprite:setPercentage(percent)
    -- print("_playPgAction", exp, lv, expMax, percent)

    local act
    if self._preLv ~= lv then
        self:_playEffect(1)
        if not self._curLegend:isMaxLv() then
            self._preLv = lv
            local percent1 = 100
            local percent2 = percent
            local time2 = exp/self._itemExp * EXP_ACTION_GAP
            local time1 = EXP_ACTION_GAP - time2
            local pg1 = cca.progressTo(time1, percent1)
            local cb = cca.cb(function() self._lvPgSprite:setPercentage(0) end)
            local pg2 = cca.progressTo(time2, percent2)
            act = cca.seq({pbg, cb, pg2})
        else
            self._preLv = nil
        end
    end
    act = act or cca.progressTo(EXP_ACTION_GAP, percent)
    self._lvPgSprite:runAction(act)
end

function ClassRef:_setPg(exp, lv, expMax)
    local percent = 100
    if expMax > 0 then 
        percent = exp/expMax * 100
        self._lvExpText:setString(string.format("%s/%s", exp, expMax))
    else
        self._lvExpText:setString(L("LegendBasic1_ui4"))
    end
    self._lvText:setString(lv)
    self._lvPgSprite:setPercentage(percent)
end

function ClassRef:refreshLvPanel()
    local legend = self._curLegend
    if self._preLv then
        self:_playPgAction(legend.exp, legend.lv, legend.expMax)
    else
        if not legend:isMaxLv() then
            self._preLv = legend.lv
        end
        self:_setPg(legend.exp, legend.lv, legend.expMax)
    end

    local lvCostItem = legend:getExpCostItem()
    self._lvItemOK = lvCostItem:getAmount() > 0
    self._lvIconRender:setData(lvCostItem)

    self._itemExp = tonumber(lvCostItem:getUseValue())
    self._lvMaxNum = lvCostItem:getAmount()

    -- print("self._itemExp", self._itemExp)
end

function ClassRef:refreshCorePanel()
    local legend = self._curLegend
    local coreSkill = legend.coreSkill
    self._coreSkillIcon:loadTexture(legend:getIcon())
    self._coreSkillNameText:setString(coreSkill:getName())

    local curTltText = self._coreCurPanel:getChildByName("tlt")
    local curDescText = self._coreCurPanel:getChildByName("text")
    curTltText:setString(string.format(L("LegendBasic2_ui1"), legend:getCoreSkillLv()))
    curDescText:setString(coreSkill:getDesc())

    local coreNextSkill = legend.coreNextSkill
    self._coreNextPanel:setVisible(nil ~= coreNextSkill)
    if coreNextSkill then
        local nextTltText = self._coreNextPanel:getChildByName("tlt")
        local nextDescText = self._coreNextPanel:getChildByName("text")
        local tltStr = string.format(L("LegendBasic2_ui1"), legend:getCoreSkillLv() + 1)
        tltStr = tltStr .. string.format(L("LegendBasic2_ui2"), legend:getNextCoreSkillNeedLv())
        nextTltText:setString(tltStr)
        nextDescText:setString(coreNextSkill:getDesc())
    end
end

function ClassRef:_onSkillEvent(eventName, render)
    local skill = render:getData()
    if eventName == "select" then
        if self._curLegend:isSkillFull() and not skill:isLegendChoose() then
            display.pushToast(L("LegendBasic3_tips1"))
        else
            executeFunc(self._onOperate, "legend_selectSkill", self._curLegend, skill)
        end
    elseif eventName == "open" then
        if skill:getLegendCostNum() > self._curLegend.skillPoint then
            display.pushToast(L("LegendBasic3_tips2"))
        else
            executeFunc(self._onOperate, "legend_openSkill", self._curLegend, skill)
        end
    end
end

function ClassRef:refreshSkillPanel()
    local legend = self._curLegend
    self._skillTypeText:setString(legend:getSpecName() .. L("resource_87"))
    self._skillNumText:setString(string.format(L("LegendBasic3_ui3"), legend.skillPoint))

    local skills = legend:getSkills()
    self._skillSV:reloadData(skills)
end

------------------ 传奇武器时装铸造 --------------------
function ClassRef:_onClickMakeBtn()
    if not self._isLegendOK then
        display.pushToast(L("LegendFashion_tips1"))
    elseif not self._isMapOK then
        display.pushToast(L("LegendFashion_tips2"))
    elseif not self._isItemOK then
        display.pushToast(L("LegendFashion_tips3"))
    else
        -- local fashion = self._curLegend:getNextActiveFashion()
        -- executeFunc(self._onOperate, "legend_activeFashion", self._curLegend, fashion)
        self:_playEffect(2)
    end
end
function ClassRef:refreshFashionPanel()
    local fashion = self._curLegend:getNextActiveFashion()
    local isActive = self._curLegend:isActive()
    local isMax = fashion == nil

    self._makeBomPanel:setVisible(not isMax)
    self._makeCorePanel:setVisible(not isMax)
    self._makeMaxPanel:setVisible(isMax)

    self._makeBtn:setVisible(isActive)
    self._makeTip:setVisible(not isActive)

    if fashion then
        local costItems = fashion:getCostItems()
        local item, hasNum
        self._isItemOK = true

        if #costItems == 0 then
            for i,render in ipairs(self._makeItemRenders) do
                render:setVisible(false)
            end
            for i,bg in ipairs(self._makeItemBgs) do
                bg:setVisible(false)
            end

            self._makeNeedText1:setString(L("LegendFashion_ui2"))
            self._makeNeedText1:setTextColor(COLOR_CONDITION_MEET)
            self._makeNeedText2:setVisible(false)
        else
            for i,render in ipairs(self._makeItemRenders) do
                item = costItems[i]
                render:setVisible(item ~= nil)
                self._makeItemBgs[i]:setVisible(item ~= nil)
                if item then
                    hasNum = UD:getItemAmountByTid(item:getId())
                    render:setData(item)
                    render:setHasAmount(hasNum)
                    self._isItemOK = self._isItemOK and hasNum >= item:getAmount()
                end
            end

            local limitLegendLv = fashion.limitLv
            self._isLegendOK = self._curLegend.lv >= limitLegendLv
            local condition1 = string.format(L("LegendFashion_ui3"), limitLegendLv)
            self._makeNeedText1:setString(condition1)
            self._makeNeedText1:setTextColor(self._isLegendOK and COLOR_CONDITION_MEET or COLOR_CONDITION_NOT_MEET)

            local condition2
            local limitMapLv, limitMapPg = fashion:getLimitMapStar()
            if limitMapLv > 0 and limitMapPg > 0 then
                self._isMapOK = self._legendInfo:getStarMapPg(limitMapLv) >= limitMapPg
                condition2 = string.format(L("LegendFashion_ui4"), limitMapLv, limitMapPg)

                self._makeNeedText2:setVisible(true)
                self._makeNeedText2:setString(condition2)
                self._makeNeedText2:setTextColor(self._isMapOK and COLOR_CONDITION_MEET or COLOR_CONDITION_NOT_MEET)
            else
                self._makeNeedText2:setVisible(false)
            end
        end

        -- 根据显示个数调整位置
        if self._makeNeedText2:isVisible() then
            self._makeNeedText1:setPositionY(self._makeNeedText1PosY)
        else
            self._makeNeedText1:setPositionY(self._makeNeedText1PosY/2 + self._makeNeedText2PosY/2)
        end

        self._makeTargetRender:setData(fashion:getFashion())
    end
end

function ClassRef:_hideTips()
    if self._curIdx == 1 then
        app:dispatchCustomEvent("EVENT_CLOSE_FLOAT")
    else
        self._ui.tips:setVisible(false)
    end
end

function ClassRef:_showTips(sender)
    if self._curIdx == 1 then
        local box = sender:getBoundingBox()
        local worldPos = sender:getParent():convertToWorldSpace( cc.p( box.x, box.y ) )
        box.x = worldPos.x
        box.y = worldPos.y

        local param =
        {
            tipsType = Enums.TipsType.desc,
            title = L("lua_code_text_440"),
            content = L("LegendBasic1_uitips"),
            boundingBox = box
        }
        app:dispatchCustomEvent("EVENT_OPEN_FLOAT", { params=param })
    else
        self._ui.tips:setVisible(true)
        local tipPanel = self._ui.tips:getChildByName("Panel")
        tipPanel:getChildByName("img_1"):setVisible(self._curIdx == 3)
        tipPanel:getChildByName("img_2"):setVisible(self._curIdx == 2)

        local tipText = tipPanel:getChildByName("text")
        if not self._tipTextSize then
            self._tipSize = tipPanel:getContentSize()
            self._tipTextSize = tipText:getContentSize()
        end
        tipText:setTextAreaSize(cc.size(self._tipTextSize.width, 0))
        tipText:setString(self._curIdx == 2 and L("LegendBasic2_uitips") or L("LegendBasic3_uitips"))

        local textSize = tipText:getVirtualRendererSize()
        tipText:setContentSize(textSize)
        local size = self._tipSize
        local diff = textSize.height - self._tipTextSize.height
        if diff > 0 then
            size = cc.size(self._tipSize.width, self._tipSize.height + diff)
        end
        tipPanel:setContentSize(size)
        ccui.Helper:doLayout(tipPanel)
    end
end

-- 播放特效
-- tp 1(核心升级) 2（外显升级）
function ClassRef:_playEffect(tp)
    local armature = self._lvupArmature
    if not armature then
        armature = Armature.create(Res.LegendEfectParam)
        self._ui.Panel:addChild(armature)
        self._lvupArmature = armature
    end

    armature:setPosition(tp == 1 and self._lvAniPos or self._makeAniPos)
    armature:setVisible(true)
    local ani = {
        name = Res.LegendEfectParam.armatureName,
        playTimes = 1,
        completeCallback = function(sender)
            sender:setVisible(false)
            if tp == 2 then
                local fashion = self._curLegend:getNextActiveFashion()
                executeFunc(self._onOperate, "legend_activeFashion", self._curLegend, fashion)
            end
        end
    }
    armature:playAni(ani)
end

return ClassRef
